var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    currentTab: 0, 
    orderList:[],
    orderonfirmed: 0,
    orderPayment: 0,
    orderDaifahuo: 0,
    orderDelivery: 0,
    orderReceipt: 0,
    orderEvaluate: 0,
  },
  onShow:function(){
    this.setData({
      currentTab: 0
    })
    this.myRequest(1)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });  
  },

  myRequest:function(orderState){
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data
        wx.showLoading({
          title: '加载中',
        })
        wx.request({
          url: app.globalData.URL + '/app/myOrder/fxOrderList?orderState='+ orderState + '&openid='+ openid,
          success: function (res) {
            wx.hideLoading()
            console.log(res.data)
            that.setData({
              orderList: res.data.fxOrderList,
              orderonfirmed: res.data.orderCount.orderonfirmed || 0,
              orderPayment: res.data.orderCount.orderPayment || 0,
              orderDaifahuo: res.data.orderCount.orderDaifahuo || 0,
              orderDelivery: res.data.orderCount.orderDelivery || 0,
              orderReceipt: res.data.orderCount.orderReceipt || 0,
              orderEvaluate: res.data.orderCount.orderEvaluate || 0,
            })
          }
        })
      },
    })
  },

  // 滑动切换tab
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
  },

  // 点击tab切换
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    if(that.data.currentTab == 0){
      that.myRequest(1)

    } else if (that.data.currentTab == 1){
      that.myRequest(2)

    } else if (that.data.currentTab == 2) {
      wx.getStorage({
        key: 'openid',
        success: function(res) {
          let openid = res.data
          wx.request({
            url: app.globalData.URL + '/app/myOrder/fxOrderList?orderState=4&remarks=3&openid='+openid,
            success: function (res) {
              console.log(res.data)
              that.setData({
                orderList: res.data.fxOrderList
              })
            }
          })
        },
      })
      
    } else if (that.data.currentTab == 3) {
      that.myRequest(5)
      
    } else if (that.data.currentTab == 4) {
      that.myRequest(6)
    }
  },

  cancleOrder:function(e){
    console.log("e:",e)
    var that = this
    wx.showModal({
      title: '',
      content: '确认取消吗?',
      success:function(res){
        if(res.cancel){

        }else{
          wx.getStorage({
            key: 'openid',
            success: function (res) {
              let openid = res.data
              wx.request({
                url: app.globalData.URL + '/app/myOrder/orderState?orderState=0&id=' + e.currentTarget.id + '&openid=' + openid,
                success: function (res) {
                  console.log(res.data)
                  if (res.data.flag == true) {
                    wx.showToast({
                      title: '取消成功',
                      success: function () {
                        that.myRequest(1)
                      }
                    })
                  }
                }
              })
            }
          })
        }
      }
    })  
  },

  toPay:function(e){
    console.log(e)
    wx.navigateTo({
      url: '../fxOrderDetail/fxOrderDetail?id=' + e.currentTarget.id,
    })
  },
  qrsh:function(e){
    console.log(e)
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        let openid = res.data
        wx.request({
          url: app.globalData.URL + '/app/myOrder/orderGoods?id=' + e.currentTarget.id + '&openid=' + openid,
          success: function (res) {
            console.log(res.data)
            if (res.data.flag == true) {
              wx.showToast({
                title: '收货成功',
                success: function () {
                  that.myRequest(5)
                }
              })
            }

          }
        })
      },
    })
  },
  toDetail:function(e){
    console.log("detail",e)
    wx.navigateTo({
      url: '../memberOrderDetail/memberOrderDetail?id=' + e.currentTarget.dataset.id + '&state=' + e.currentTarget.dataset.state,
    })
  }
})